package com.teamcenter.rac.massupdate.model;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;

import com.teamcenter.rac.aif.AbstractAIFOperation;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.massupdate.providers.PropertySourceLabelProvider;
import com.teamcenter.rac.massupdate.utils.MassUpdateHelper;
import com.teamcenter.rac.massupdate.utils.SourceHelper;
@SuppressWarnings({"unused","rawtypes","unchecked"})
public class MarkupModel extends AbstractWrappedPropertySource {
	public static final String MARKUP_COMMENT_PROP = "fnd0comment";
	public static final String MARKUP_PURPOSE_PROP = "fnd0purpose";
	public static final String PROP_CHANGE_ITEM_REV = "changeItemRevision";
	public static final String PROP_MARKUP = "markup";
	public static final String PROP_CHANGE_EXECUTION_ALLOWED = "changeExecutionAllowed";
	public static final String PROP_CHANGE_UPDATE_ALLOWED = "changeUpdateAllowed";
	private static final int NUM_LOCAL_PROP = 4;
	private List m_markupChanges;

	public MarkupModel(IPropertySource paramIPropertySource) {
		super(4);
		this.m_localPropNames.add("markup");
		this.m_localPropNames.add("changeItemRevision");
		this.m_localPropNames.add("changeExecutionAllowed");
		this.m_localPropNames.add("changeUpdateAllowed");
		this.m_markupChanges = new WritableList(new ArrayList<>(), MarkupChangeModel.class);
		setMarkup(paramIPropertySource);
	}

	public void setMarkupChanges(List paramList) {
		this.m_markupChanges = paramList;
	}

	public void addMarkupChange(MarkupChangeModel paramMarkupChangeModel) {
		this.m_markupChanges.add(paramMarkupChangeModel);
	}

	public IPropertySource getChangeItemRevision() {
		return (IPropertySource) getPropertyValue("changeItemRevision");
	}

	public String getFnd0comment() {
		return (String) getPropertyValue("fnd0comment");
	}

	public String getFnd0purpose() {
		return (String) getPropertyValue("fnd0purpose");
	}

	public IPropertySource getMarkup() {
		return (IPropertySource) getPropertyValue("markup");
	}

	public List getMarkupChanges() {
		return this.m_markupChanges;
	}

	public IPropertyDescriptor[] getPropertyDescriptors() {
		if (this.m_propertyDescriptors == null) {
			List localList = getWrappedPropertyDescriptors(4);
			PropertyDescriptor localPropertyDescriptor1 = new PropertyDescriptor("markup",
					Messages.MarkupModel_Prop_Markup);
			localPropertyDescriptor1.setLabelProvider(new PropertySourceLabelProvider("markup"));
			localList.add(localPropertyDescriptor1);
			PropertyDescriptor localPropertyDescriptor2 = new PropertyDescriptor("changeItemRevision",
					Messages.UpdateJobModel_Prop_ChangeObject);
			localPropertyDescriptor2.setLabelProvider(new PropertySourceLabelProvider("changeItemRevision"));
			localList.add(localPropertyDescriptor2);
			this.m_propertyDescriptors = ((IPropertyDescriptor[]) localList
					.toArray(new IPropertyDescriptor[localList.size()]));
		}
		return this.m_propertyDescriptors;
	}

	public void removeMarkupChange(MarkupChangeModel paramMarkupChangeModel) {
		this.m_markupChanges.remove(paramMarkupChangeModel);
	}

	public void setChangeItemRevision(IPropertySource paramIPropertySource) {
		setChangeExecutionAllowed(paramIPropertySource);
		setChangeUpdateAllowed(paramIPropertySource);
		updateAndAnnounceProperty("changeItemRevision", paramIPropertySource);
	}

	public void setFnd0comment(final String paramString) {
		setPropertyValue("fnd0comment", paramString);
		if (getMarkup() != null) {
			final TCComponent localTCComponent = SourceHelper.getAssociatedComponent(getMarkup(), getTCSession());
			if (localTCComponent != null) {
				AbstractAIFOperation local1 = new AbstractAIFOperation() {
					public void executeOperation() throws Exception {
						localTCComponent.setProperty("fnd0comment", paramString);
					}
				};
				getTCSession().queueOperation(local1);
			}
		}
	}

	public void setFnd0purpose(String paramString) {
		setPropertyValue("fnd0purpose", paramString);
	}

	public void setMarkup(IPropertySource paramIPropertySource) {
		setWrappedPropertySource(paramIPropertySource);
		updateAndAnnounceProperty("markup", paramIPropertySource);
	}

	private void setChangeExecutionAllowed(IPropertySource paramIPropertySource) {
		TCComponent localTCComponent = SourceHelper.getAssociatedComponent(paramIPropertySource, getTCSession());
		boolean bool = MassUpdateHelper.evaluateCondition("isChangeExecutionAllowed",
				new TCComponent[] { localTCComponent }, getTCSession());
		updateAndAnnounceProperty("changeExecutionAllowed", Boolean.valueOf(bool));
	}

	public boolean isChangeExecutionAllowed() {
		return getPropertyValueAsBoolean("changeExecutionAllowed");
	}

	private void setChangeUpdateAllowed(IPropertySource paramIPropertySource) {
		TCComponent localTCComponent = SourceHelper.getAssociatedComponent(paramIPropertySource, getTCSession());
		boolean bool = MassUpdateHelper.evaluateCondition("isCMHasImpactedItemCreatableForPrimary",
				new TCComponent[] { localTCComponent }, getTCSession());
		updateAndAnnounceProperty("changeUpdateAllowed", Boolean.valueOf(bool));
	}

	public boolean isChangeUpdateAllowed() {
		return getPropertyValueAsBoolean("changeUpdateAllowed");
	}
}